package top.lgd.base;

import lombok.Getter;
import top.lgd.enums.ResultEnum;

import java.io.Serializable;

/**
 * @author YYJ
 * @version 1.0
 * @since 2023/12/15 9:53
 */
@Getter
public class LgdResult<D> implements Serializable {

    private static final long serialVersionUID = 6766792096708655917L;

    private boolean success;
    private String code;
    private String msg;
    private D data;

    private LgdResult(String code, String msg, D data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static LgdResult success() {
        return success(ResultEnum.SUCCESS.getMsg(), null);
    }

    public static <D> LgdResult success(D data) {
        return success(ResultEnum.SUCCESS.getMsg(), data);
    }

    public static <D> LgdResult success(String msg, D data) {
        return new LgdResult(ResultEnum.SUCCESS.getCode(), msg, data);
    }

    public static LgdResult fail() {
        return new LgdResult(ResultEnum.FAIL.getCode(), ResultEnum.FAIL.getMsg(), null);
    }

    public static LgdResult fail(ResultEnum resultEnum) {
        return new LgdResult(resultEnum.getCode(), resultEnum.getMsg(), null);
    }

    public static LgdResult fail(String msg) {
        return new LgdResult(ResultEnum.FAIL.getCode(), msg, null);
    }

    public boolean isSuccess() {
        return ResultEnum.SUCCESS.getCode().equals(code) ? true : false;
    }
}